//+--------------------------------------------------------------------------------
//| Copyright (c) 2012 - 2013 ### an.vuongngocduy
//| This file is a part of Compact Game Engine (CGE)
//+--------------------------------------------------------------------------------
#ifndef __CGE_SINGLETON_H__
#define __CGE_SINGLETON_H__

namespace cge
{
	namespace std
	{
		template<class TVal>
		class Singleton
		{
		public:
			//! Destructor
			~Singleton()
			{
				if(Singleton<TVal>::sInstance != null)
					delete Singleton<TVal>::sInstance;
			}


			//! Get instance of singleton
			static TVal* Instance()
			{
				if(Singleton<TVal>::sInstance == null)
				{
					Singleton<TVal>::sInstance = new TVal*[1];
					Singleton<TVal>::sInstance[0] = new TVal();
				}
				return Singleton<TVal>::sInstance[0];
			}


		protected:
			//! Default constructor
			Singleton()
			{
				if(sInstance == null)
				{
					// Error : Call constructor directly
					//TODO : Put an ASSERT or EXCEPTION here
				}
				else
				{
					if(sInstance[0] != null)
					{
						// Error: Duplicate singleton
						//TODO : Put an ASSERT or EXCEPTION here
					}
				}
			}

		protected:
			static TVal** sInstance;
		};

		template<class TVal>
		TVal** Singleton<TVal>::sInstance = null;
	}
}

#endif//__CGE_SINGLETON_H__